package cn.molu.jsoup.controller;

import cn.hutool.core.util.StrUtil;
import cn.molu.jsoup.service.ParseHtmlService;
import cn.molu.jsoup.vo.ResultVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Map;

/**
 * @author 陌路
 * @apiNote 解析网页资源控制器
 * @date 2024/1/16 14:03
 * @tool Created by IntelliJ IDEA
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/parseHtml")
public class ParseHtmlController {

    private final ParseHtmlService parseHtmlService;

    /**
     * 根据关键字爬取数据
     *
     * @param keyword 搜索关键字
     * @return 结果集
     */
    @GetMapping("/query/{keyword}")
    public ResultVo<Collection<Map<String, String>>> parseHtml(@PathVariable String keyword) {
        if (StrUtil.isBlank(keyword)) return ResultVo.fail("请输入搜索关键字!");
        return ResultVo.ok(parseHtmlService.parseHtmlByKeyword(keyword));
    }
}
